package com.xhui.vimarket.zkplus.zul.attr.mod.impl;t java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.directwebremoting.util.Logger;
import org.zkoss.zk.ui.Component;
import org.zkoss.zul.Div;
import org.zkoss.zul.Hbox;
import org.zkoss.zul.Label;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Span;

import com.xhui.crm.common.util.numeric.NumericUtil;
import com.xhui.crm.common.util.string.StrUtil;
import com.xhui.crm.common.vo.NodeVo;
import com.xhui.crm.common.vo.RetVO;com.xhui.vimarket.zkplusui.vimarket.zkplus.ui.id.ZKRebcom.xhui.vimarket.zkplusrt com.xhui.vimarket.zkpcom.xhui.vimarket.zkplusls;
import com.xhui.vimcom.xhui.vimarket.zkplusil.ZkUiUtil;
import com.xhcom.xhui.vimarket.zkpluslus.zul.attr.AttrExt;
import comcom.xhui.vimarket.zkpluszkplus.zul.attr.bean.AttrInst;
icom.xhui.vimarket.zkplusvimarket.zkplus.zul.attr.bean.Acom.xhui.vimarket.zkplust com.xhui.vimarket.zkplus.zul.attr.bean.com.xhui.vimarket.zkplus com.xhui.vimarket.zkplus.zul.attr.mcom.xhui.vimarket.zkplustants;
import com.xhui.vimarket.zkplus.zul.attr.mod.AttrCompUtil;
impocom.xhui.vimarket.zkplusarket.zkplus.zul.attr.util.AttrConstants;

/**
 * @版权：福富软件 版权所有 (c) 2010
 * @文件：com.xhui.vimarket.zkplus.zul.attr.mod.impl.AttrSelectlistComp.java
 * @所含类：
 * @author: wuyx
 * @version: V1.0
 * @see:
 * @创建日期：2010-12-20
 * @功能说明：属性控件接口
 * @修改记录： =============================================================<br>
 *        日期:2010-12-20 创建
 *        =============================================================<br>
 */
public final class AttrSelectlistComp extends AttrBaseComp {
    
    /**
     * 实例.
     */
    private static AttrSelectlistComp inst           = new AttrSelectlistComp();
    /**
     * 日志.
     */
    private static Logger             logger         = Logger.getLogger(AttrSelectlistComp.class);
    
    /**
     * 展示当前有效值.
     */
    public static final String        SHOW_EFF_VALUE = "showEff";
    /**
     * 展现权限判断.
     */
    public static final String        SHOW_QX        = "QX";
    
    /**
     * 构造函数. 
     */
    private AttrSelectlistComp() {
    }
    
    /**
     * 获取实例.
     * 
     * @return AttrBaseComp
     */
    public static AttrSelectlistComp instance() {
        return AttrSelectlistComp.inst;
    }
    
    /**
     * 生成组件.
     * 
     * @param attrExt 组件控件
     * @param attrTmpl 组件模板
     * @param comp 显示容器组件
     */
    @SuppressWarnings("deprecation")
    @Override
    public void create(final AttrExt attrExt, final AttrTmpl attrTmpl, final Component comp) {
        super.create(attrExt, attrTmpl, comp);
        // panel
        final Hbox hbox = new Hbox();
        hbox.setSclass("zh-hbox-widths");
        hbox.setAttribute(AttrBaseComp.COMP_MODE, attrTmpl.getDisplayMode());
        hbox.setAttribute(AttrBaseComp.COMP_ATTRID, attrTmpl.getAttrId());
        hbox.setAttribute(AttrBaseComp.COMP_JAVACODE, attrTmpl.getJavaCode());
        hbox.setAttribute(AttrBaseComp.COMP_CANTNULL, AttrCompUtil.getStrCantNull(attrTmpl
            .isCantNull()));
        hbox.setAttribute(AttrBaseComp.COMP_COMP_NAME, attrTmpl.getName());
        AttrCompUtil.setCompAttr(hbox, AttrBaseComp.COMP_VALUE_LENGTH, attrTmpl.getValueLength());
        AttrCompUtil.setCompAttr(hbox, AttrBaseComp.COMP_VALUE_FROM, attrTmpl.getValueFrom());
        AttrCompUtil.setCompAttr(hbox, AttrBaseComp.COMP_VALUE_TO, attrTmpl.getValueTo());
        AttrCompUtil.setCompAttr(hbox, AttrBaseComp.COMP_VALUE_REX, attrTmpl.getValidExpr());
        hbox.setWidth("100%");
        hbox.setWidths(this.getWidthsLCT(attrTmpl));
        final Div labDiv = new Div();
        final Label label = new Label(attrTmpl.getName());
        label.setSclass("zp-lt-label");
        // info
        final Span infDiv = this.createRulebar(attrExt, labDiv, attrTmpl);
        // add to zul
        hbox.setParent(comp);
        labDiv.setParent(hbox);
        label.setParent(labDiv);
        infDiv.setParent(labDiv);
        
        // listbox.setParent(hbox);
        // final Listbox listbox = new Listbox();
        /*
        final String sListboxWithComposer = attrExt.getAttrAda().getAttrTmplCompStr(
            attrTmpl.getDisplayMode(), attrTmpl, attrTmpl.getParam2(), null, null);
        final Listbox listbox = (Listbox) Executions.createComponentsDirectly(sListboxWithComposer,
            null, hbox, null);
        */
        final Listbox listbox = (Listbox) attrExt.getAttrAda().getAttrTmplCompStr(
            attrTmpl.getDisplayMode(), attrTmpl, attrTmpl.getParam2(), null, null, hbox);
        //listbox.setParent(hbox);
        listbox.setMold("select");
        listbox.setWidth(AttrBaseComp.WIDTH_SELECT);
        ZKRebuildUuid.rebuildUuidById(listbox, "rd_list_"
            + (attrTmpl.getAttrId() > 0 ? attrTmpl.getAttrId()
                : "_" + Math.abs(attrTmpl.getAttrId())));
        if (attrExt.getAttrAda().isViewMode()) { // 展示模式
            listbox.setDisabled(true);
        }
        if (attrTmpl.isReadOnly()) {
            listbox.setDisabled(true);
        }
        if (attrTmpl.getHint() != null) {
            hbox.setTooltiptext(attrTmpl.getHint());
        }
        if (!attrTmpl.isDisplay()
            || AttrConstants.DISPLAY_STYLE_PANEL_HIDE.equals(attrTmpl.getDisplayStyle())) {
            hbox.setVisible(false); // 隐藏
        }
        final Listitem listitem1 = new Listitem();
        listitem1.setLabel("请选择...");
        listitem1.setValue("");
        listitem1.setParent(listbox);
        ZKRebuildUuid.rebuildUuidById(listitem1, "rd_def_"
            + (attrTmpl.getAttrId() > 0 ? attrTmpl.getAttrId()
                : "_" + Math.abs(attrTmpl.getAttrId())));
        if (attrTmpl.getAttrTmplList() != null && attrTmpl.getAttrTmplList().size() > 0) { // 界面直接配置
            for (int i = 0; i < attrTmpl.getAttrTmplListCount(); i++) {
                final AttrTmpl subAttrTmpl = attrTmpl.getAttrTmplList(i);
                final Listitem listitem = new Listitem();
                //重构Uuid用于压力测试, 由于item.getId不一定合法。
                ZKRebuildUuid.rebuildUuidById(listitem, "rd_"
                    + (attrTmpl.getAttrId() > 0 ? attrTmpl.getAttrId()
                        : "_" + Math.abs(attrTmpl.getAttrId())));
                listitem.setLabel(subAttrTmpl.getName());
                listitem.setValue(subAttrTmpl.getParam1());
                listitem.setParent(listbox);
            }
        } else if (attrTmpl.getValueList() != null && attrTmpl.getValueList().size() > 0) { // 底层配置
            for (int i = 0; i < attrTmpl.getValueList().size(); i++) {
                final NodeVo item = attrTmpl.getValueList().get(i);
                final Listitem listitem = new Listitem();
                //重构Uuid用于压力测试, 由于item.getId不一定合法。
                ZKRebuildUuid.rebuildUuidById(listitem, "rd_"
                    + (attrTmpl.getAttrId() > 0 ? attrTmpl.getAttrId()
                        : "_" + Math.abs(attrTmpl.getAttrId())));
                if (!StrUtil.isNullOrEmpty(item.getType())) {
                    listitem.setAttribute(SHOW_QX, item.getType());
                }
                listitem.setLabel(item.getName());
                listitem.setValue(item.getId());
                listitem.setParent(listbox);
            }
        }
        
        // toolbox
        this.createToolbox(hbox, attrExt.isToolboxVisible(), attrTmpl);
        
        // comp log
        if (AttrSelectlistComp.logger.isDebugEnabled()) {
            attrExt.appendInnerComp("<hbox width=\"100%\" widths=\"40%,60%\" >");
            attrExt.appendInnerCompAttr(AttrBaseComp.COMP_MODE, attrTmpl.getDisplayMode());
            attrExt.appendInnerCompAttr(AttrBaseComp.COMP_ATTRID, "" + attrTmpl.getAttrId());
            attrExt.appendInnerCompAttr(AttrBaseComp.COMP_JAVACODE, "" + attrTmpl.getJavaCode());
            attrExt.appendInnerComp("<label value=\"");
            attrExt.appendInnerComp(attrTmpl.getName());
            attrExt.appendInnerComp("\" />");
            attrExt.appendInnerComp("<listbox ");
            if (attrTmpl.isReadOnly()) {
                attrExt.appendInnerComp("readonly=\"true\"");
            }
            attrExt.appendInnerComp(">");
            if (attrTmpl.getValueList() != null && attrTmpl.getValueList().size() > 0) {
                for (int i = 0; i < attrTmpl.getValueList().size(); i++) {
                    final NodeVo item = attrTmpl.getValueList().get(i);
                    attrExt.appendInnerComp("<listitem value=\"");
                    attrExt.appendInnerComp(item.getId());
                    attrExt.appendInnerComp("\" label=\"");
                    attrExt.appendInnerComp(item.getName());
                    attrExt.appendInnerComp("\" />");
                }
            }
            attrExt.appendInnerComp("</listbox></hbox>");
        }
        
        super.afterCreate(attrExt, attrTmpl, comp);
    }
    
    /**
     * 设置值.
     * 
     * @param attrExt 组件控件
     * @param comp 组件
     * @param attrInstList 属性实例
     */
    @Override
    public void setValue(final AttrExt attrExt, final Component comp,
        final List<AttrInst> attrInstList, final Map<String, Integer> attrViewMap) {
        super.setValue(attrExt, comp, attrInstList, attrViewMap);
        //列表
        final Component compListbox = getListbox(comp);
        if (compListbox != null) {
            final long theAttrId = NumUtils
                .toLong("" + comp.getAttribute(AttrBaseComp.COMP_ATTRID));
            final ArrayList<AttrInst> theAttrList = AttrBaseComp.getAttrInstListById(attrInstList,
                theAttrId);
            if (theAttrList.size() > 0) {
                AttrSelectlistComp.selectByCodeValue((Listbox) compListbox, theAttrList.get(0)
                    .getParam1(), theAttrList.get(0));
                if (this.SHOW_EFF_VALUE.equalsIgnoreCase(("" + comp
                    .getAttribute(AttrBaseComp.COMP_PARAM1)))
                    && theAttrList.get(0).getEffParam() != null
                    && !theAttrList.get(0).getEffParam().equals(theAttrList.get(0).getParam1())) {
                    selectEffByCodeValue((Listbox) compListbox, theAttrList.get(0).getEffParam());
                }
                // javacode回填
                AttrCompUtil.copyTmplInfo(theAttrList.get(0), comp);
            }
        }
    }
    
    /* @Override
     public void setEffValue(final AttrExt attrExt, final Component comp,
         final List<AttrInst> attrInstList) {
         //列表
         final Component compListbox = getListbox(comp);
         if (compListbox != null) {
             final long theAttrId = NumUtils
                 .toLong("" + comp.getAttribute(AttrBaseComp.COMP_ATTRID));
             final ArrayList<AttrInst> theAttrList = AttrBaseComp.getAttrInstListById(attrInstList,
                 theAttrId);
             if (theAttrList.size() > 0) {
                 if (this.SHOW_EFF_VALUE.equalsIgnoreCase(("" + comp.getAttribute(AttrBaseComp.COMP_PARAM1)))) {
                     String effValue = null;
                     if (!theAttrList.get(0).getParam1().equals(effValue)) {
                         selectEffByCodeValue((Listbox) compListbox, effValue);
                     }          
                 }
             }
         }
     }*/
    /**
     * 获取值.
     * 
     * @param attrExt 组件控件
     * @param comp 组件
     * @return ArrayList<AttrInst>
     */
    @Override
    public ArrayList<AttrInst> getValue(final AttrExt attrExt, final Component comp) {
        super.getValue(attrExt, comp);
        final ArrayList<AttrInst> retList = new ArrayList<AttrInst>();
        // 取列表
        final Component compListbox = getListbox(comp);
        if (compListbox != null) {
            final Listitem selectedItem = ((Listbox) compListbox).getSelectedItem();
            if (selectedItem != null && !"".equals(StrUtil.strnull(selectedItem.getValue()))) { // 有选中值
                final AttrInst attrInst = attrExt.newAttrInst();
                attrInst.setAttrId(NumUtils
                    .toLong("" + comp.getAttribute(AttrBaseComp.COMP_ATTRID)));
                attrInst
                    .setJavaCode(StrUtil.strnull(comp.getAttribute(AttrBaseComp.COMP_JAVACODE)));
                if (!StrUtil.isNullOrEmpty(selectedItem.getAttribute(SHOW_QX)) 
                    && SHOW_QX.equals(selectedItem.getAttribute(SHOW_QX))) {
                    attrInst.setCheckRight(true);
                }
                attrInst.setAttrName(selectedItem.getLabel());
                attrInst.setParam1(StrUtil.strnull(selectedItem.getValue()));
                attrInst.setOp(this.getToolbarOp(comp));
                retList.add(attrInst);
         com.xhui.vimarket.zkplus
        return retList;
        
    }
 com.xhui.vimarket.zkplus    * {@inheritDoc}
     * @see com.xhui.vimarket.zkplus.zul.attr.mod.impl.AttrBaseComp#signRuleHint(com.xhui.vimarket.zkplus.zul.attr.AttrExt, org.zkoss.zk.ui.Component)
     * @author wuyx
     * 2012-2-16 wuyx
     */
    @Override
    public ArrayList<ValiVo> signRuleHint(AttrExt attrExt, Component comp) {
        super.signRuleHint(attrExt, comp);
        if (AttrCompUtil.isCantNull(comp)) { // 必填
            ArrayList<AttrInst> valList = this.getValue(attrExt, comp);
            if (valList == null || valList.size() < 1) {
                ValiVo retVo = new ValiVo();
                ArrayList<ValiVo> retVoList = new ArrayList<ValiVo>();
                retVoList.add(retVo);
                return retVoList;
            }
        }
        return null;
    }
    
    /**
     * 取空值属性实例.
     * 
     * @param attrExt 组件控件
     * @param attrTmpl 模板
     * @return ArrayList<AttrInst>
     */
    @Override
    public ArrayList<AttrInst> getNullValue(final AttrExt attrExt, final AttrTmpl attrTmpl) {
        super.getNullValue(attrExt, attrTmpl);
        final ArrayList<AttrInst> retList = new ArrayList<AttrInst>();
        final AttrInst attrInst = attrExt.newAttrInst();
        attrInst.setAttrId(attrTmpl.getAttrId());
        attrInst.setJavaCode(attrTmpl.getJavaCode());
        retList.add(attrInst);
        return retList;
    }
    
    /**
     * selectByCodeValue.
     * @param listbox Listbox
     * @param value String
     */
    public static void selectByCodeValue(final Listbox listbox, final String value,
        final AttrInst attrInst) {
        if (listbox == null) {
            throw new IllegalArgumentException("listbox must not null!");
        }
        if (value == null) {
            return;
        }
        for (final Object object : listbox.getItems()) {
            final Listitem item = (Listitem) object;
            final String itemValue = (String) item.getValue();
            if (itemValue != null && itemValue.equals(value)) {
                //listbox.selectItem(null);
                item.setSelected(true);
                return;
            }
        }
        // 有值的时候需要将值添加到可选属性
        if (!StrUtil.isNullOrEmpty(value)) {
            final Listitem listitem = new Listitem();
            //重构Uuid用于压力测试, 由于item.getId不一定合法。
            ZKRebuildUuid.rebuildUuidById(listitem, "rd_" + attrInst.getAttrId()
                + attrInst.getAttrInstId());
            listitem.setLabel("属性值（" + value + "）");
            listitem.setValue(value);
            listitem.setParent(listbox);
            listitem.setSelected(true);
        }
        // 没有设置到值时候，设置到默认的“请选择”
        else if (listbox.getItemCount() > 0) {
            listbox.setSelectedIndex(0);
        }
    }
    
    /**
     * selectEffByCodeValue.
     * @param listbox Listbox
     * @param value String
     */
    public static void selectEffByCodeValue(final Listbox listbox, final String value) {
        if (listbox == null) {
            throw new IllegalArgumentException("listbox must not null!");
        }
        //没有当前有效值时，默认为请选择
        if (value == null) {
            listbox.getItemAtIndex(0).setStyle("color:#F26203");
            return;
        }
        for (final Object object : listbox.getItems()) {
            final Listitem item = (Listitem) object;
            final String itemValue = (String) item.getValue();
            if (itemValue != null && itemValue.equals(value)) {
                item.setStyle(com.xhui.vimarket.zkplus);
                return;
           com.xhui.vimarket.zkplus    }
    
    /**
     * {@inheritDoc}
     * @see com.xhui.vimarket.zkplus.zul.attr.mod.AttrComp
     * #getComp(com.xhui.vimarket.zkplus.zul.attr.AttrExt, org.zkoss.zk.ui.Component)
     * @author wuyx
     * 2011-1-24 wuyx
     */
    @Override
    public List<Component> getComp(final AttrExt attrExt, final Component comp, final long attrId) {
        final List<Component> retList = new ArrayList<Component>();
        // 取列表
        final Object oAttrId = comp.getAttribute(AttrBaseComp.COMP_ATTRID);
        if (oAttrId == null || "".equals(oAttrId)) {
            return null;
        } else if (attrId == NumericUtil.toLoncom.xhui.vimarket.zkplus) {
            retList.add(comp);
   com.xhui.vimarket.zkplus return retList;
    }
    
    /**
     * {@inheritDoc}
     * @see com.xhui.vimarket.zkplus.zul.attr.mod.AttrComp
     * #getComp(com.xhui.vimarket.zkplus.zul.attr.AttrExt, org.zkoss.zk.ui.Component)
     * @author wuyx
     * 2011-1-24 wuyx
     */
    @Override
    public List<Component> getComp(final AttrExt attrExt, final Component comp,
        final String javaCode) {
        final List<Component> retList = new ArrayList<Component>();
        // 取列表
        final Object oAttrId = comp.getAttribute(AttrBaseComp.COMP_JAVACODE);
        if (oAttrId == null || "".equals(oAttrId)) {
            
        }
        return retList;
    }
    
    /**
     * getListbox.
     * 
     * @param root Component
     * @return Listbox
     */
    public static Listbox getListbox(Component root) {
        // 取输入框
        // hbox // Listbox(1) 
        return (Listbox) root.getFirstChild().getNextSibling();
    }
    
    /**
     * getHideLabel.
     * 
     * @param root Component
     * @return Label
     */
    public static Label getHideLabel(Component root) {
        // 隐藏信息
        // hbox // Listbox(1) 
        return (Label) root.getLastChild();
    }
    
    /**
     * 设置为不允许修改.
     * 
     * @param attrExt 组件控件
     * @param comp 组件
     * @param tflag 设置值，true/false
     */
    @Override
    public void setDisabled(AttrExt attrExt, Component comp, boolean tflag) {
   com.xhui.vimarket.zkplusisabled(attrExt, comp, tflag);
        ZkUiUtil.setDisabled(comp, tflag);
    }
    
    @Override
    /**
     * {@inheritDoc}
     * @see com.xhui.vimarket.zkplus.zul.attr.mod.AttrComp
     * #setAttrView(org.zkoss.zk.ui.Component, Map<String, Integer>)
     * @author chenmh
     * 2011-6-16 chenmh
     */
    public void setAttrView(final Component comp, Map<String, Integer> attrViewMap) {
        if (attrViewMap != null && attrViewMap.size() > 0) {
            String attrJC = comp.getAttribute(AttrBaseComp.COMP_JAVACODE) + "";
            if (!StrUtil.isNullOrEmpty(attrJC)) {
                Integer viewType = attrViewMap.get(attrJC);
                // 获取下拉框
                final Listbox compListbox = getListbox(comp);
                if (compListbox != null) {
                    compListbox.setVisible(false);
                    if (AttrBaseComp.VIEW_TYPE_SHOW == viewType) {
                        compListbox.setDisabled(true);
                        compListbox.setVisible(true);
                    } else if (AttrBaseComp.VIEW_TYPE_EDIT == viewType) {
                        compListbox.setVisible(true);
                    }
                }
            }
        }
    }
    
    @Override
    public List<RetVO> validate(AttrExt attrExt, Component comp) {
        if (AttrCompUtil.isCantNull(comp)) { // 需限制不可空
            ArrayList<AttrInst> valList = this.getValue(attrExt, comp);
            if (valList == null || valList.size() < 1) { // 没有值
                return AttrCompUtil.getRetVoList(AttrCompConstants.RET_ERROR, AttrCompUtil
                    .getCompName(comp)
                    + AttrCompConstancom.xhui.vimarket.zkplusNULL, comp);
            }
            return null; // 通过
        }
        return null; // 通过
    }
    
    /**
     * {@inheritDoc}
     * @see com.xhui.vimarket.zkplus.zul.attr.mod.AttrComp
     * #getMsgComp(org.zkoss.zk.ui.Component)
     * @author wuyx
     * 2011-7-19 wuyx
     */
    @Override
    public Component getMsgComp(Component comp) {
        return comp.getRoot();
        //return getListbox(comp).getParent();
    }
    
    /**
     * 清空值.
     * 
     * @param attrExt 组件控件
     * @param comp 组件
     */
    @Override
    public void clean(final AttrExt attrExt, final Component comp) {
        super.clean(attrExt, comp);
        // 取输入框
        final Component compListbox = getListbox(comp);
        if (compListbox != null) {
            AttrSelectlistComp.selectByCodeValue((Listbox) compListbox, "", null);
        }
    }
}
